Deployment Status Apache License Documentation Status Python Online Python version—Jan 27, 2021


Copyright © Wei MEI, MLMS™—all rights reserved. 🀤

print

The print function allows you to send output to the terminal:

print(*objects, sep=' ', end='n', file=sys.stdout, flush=False)

Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(…) instead.

Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

Changed in version 3.3: Added the flush keyword argument.

1
2
# the print statement displays a message 
print('Hello world')

Find more details on print.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Each print statements starts on a new line
print('Hello world')

# If you pass nothing to the print statement you get a blank line
print()
print('Did you see that blank line?')

# '\n' is a special character sequence that means print new line
# you can use it to break the output over multiple lines
print('Blank line \nin the middle of string')



Strings can be enclosed in single quotes or double quotes

  • “this is a string”
  • ‘this is also a string’
1
2
3
4
5
# Strings can be enclosed in single quotes
print('Hello world single quotes')

# Strings can also be enclosed in double quotes
print("Hello world double quotes")

input

The input function allows you to prompt a user for a value:

input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised. Example:

>>> s = input('--> ')
--> Monty Python's Flying Circus
>>> s
"Monty Python's Flying Circus"

If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.

Raises an auditing event builtins.input with argument prompt before reading input

Raises an auditing event builtins.input/result with the result after successfully reading input.

1
2
3
4
5
# The input funciton allows you to prompt the user for a value
# You need to declare a variable to hold the value entered by the user
name = input('What is your name? ')

print(name)

Find more details on input.

Parameters:

  • prompt: Message to display to the user

return value:

  • string value containing value entered by user

Challenges time

There are some challenges you can try to take:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Here's a challenge for you to help you practice
# See if you can fix the code below 

# print the message
print('Why won't this line of code print')

# print the message
prnit('This line fails too!')

# print the message
print "I think I know how to fix this one"

# print the name entered by the user
input('Please tell me your name: ')
print(name)

solutions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# Here's a challenge for you to help you practice
# See if you can fix the code below 

# print the message
# There was a single quote inside the string!
# Use double quotes to enclose the string
print("Why won't this line of code print")

# print the message
# There was a mistake in the function name
print('This line fails too!')

# print the message
# Need to add the () around the string
print ("I think I know how to fix this one")

# print the name entered by the user
# You need to store the value returned by the input statement
# in a variable
name = input('Please tell me your name: ')
print(name)

Introducing Python

Before you get started on your journey towards learning Python, it’s important to know why! We’ll talk through what Python is, where you’ll use it, and how it can help you problem solve.

PPT Demonstrations